package com.qf.lja.servlet;

import com.qf.lja.dao.UserDao;
import com.qf.lja.util.JDBCUtil;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/register.do")
public class RegistrationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Retrieve form parameters
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("upassword");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String sex = request.getParameter("usex");
        Integer Sex = 0;
        if(sex.equals("男"))
        {
            Sex = 1;
        }


        // For demonstration, let's just print out the received data
        System.out.println("Received registration details:");
        System.out.println("Username: " + username);
        System.out.println("Password: " + password);
        System.out.println("Email: " + email);
        System.out.println("Sex: " + Sex);

        UserDao userDao = new UserDao();
        userDao.adduser(username, password, phone, email, 1, 0, Sex);
        // Set a message to be displayed on the registration page
        String registerMsg = "Registration successful for " + username;
        // Forward the message to the registration page
        request.setAttribute("registerMsg", registerMsg);
        request.getRequestDispatcher("/register.jsp").forward(request, response);
    }
}
